<?php

namespace app\model;

use app\stateData;
use think\Model;

class SystemDynamicFromField extends Model
{
    /**
     * @var mixed
     */
    public $id;

    /**
     *  字段表
     */

    protected $createTime = 'create_date';

    /**
     * 关联模板
     * @return \think\model\relation\HasOne
     */


//    public function powers()
//    {
//        return $this->hasOne (SystemFieldRandPower::class,'FIELD_ID','ID')->bind ([
//            'people'=>'people',
//            'position'=>'position',
//            'organization'=>'org',
//        ]);
//    }


    /**
     * 获取器
     * @param $value
     * @return string
     */
    public function getAttributeAttr($value): string
    {
        $status = [
            0=>'',
            1 => '系统字段',
            2 => '自定义字段'
        ];

        return $status[$value];
    }

//    public function getRequireAttr($value):bool
//    {
//        return (bool)$value;
//    }

//    public function getOrganizationAttr($name)
//    {
//        return $name?[$name]:'';
//    }
//    public function getPositionAttr($name)
//    {
//        return $name?[$name]:'';
//    }
//    public function getPeopleAttr($name)
//    {
//        return $name?[$name]:'';
//    }


    /**
     * 查询模板对应的所有字段
     * @param $fid
     * @return array|SystemDynamicFromField[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTmpFieldList($fid): array
    {
        return $this->where(['fid'=>$fid,'is_k' => 0])->order('attribute ASC')->select()->toArray();
    }
    public function getTmpFieldPage($fid,$page,$pageNum,$name):array
    {
        return $this->whereLike ('name','%'.$name.'%')->where(['fid'=>$fid])->page($page,$pageNum)->select()->toArray();
    }
    public function getTmpFieldCount($fid)
    {
        return $this->where(['fid'=>$fid])->count();
    }
    /**
     * 查询模板对应的自定义字段
     * @param $fid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTmpTrendsFieldList($fid):array
    {
        return $this->where (['fid'=>$fid,'attribute'=>2])->select()->toArray ();
    }

    public function getTmpSystemFieldList($fid):array
    {
        return $this->where (['fid'=>$fid,'attribute'=>1])->select()->toArray ();
    }

    /**
     * 添加字段表
     * @param $data
     * @return \app\model\SystemDynamicFromField|int|string|\think\Model
     */
    public function addFieldModel($data)
    {
        return $this->insertGetId($data);
    }

    /**
     * @param $data
     * @return \think\Collection
     * @throws \Exception
     */
    public function addFieldAllModel($data): \think\Collection
    {

        return $this->saveAll ($data);
    }
    /**
     * 修改字段表
     */
    public function updateFieldModel($data)
    {
        return self::update($data);
    }

    /**
     * 删除字段表
     * @param $id
     * @return bool
     */
    public function delFieldModel($id): bool
    {
        return self::destroy($id);
    }

    /**
     * 查询字段详情
     * @param $id
     * @return \app\model\SystemDynamicFromField[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFieldDetails($id)
    {
        $data = $this->where(['id'=>$id])->findOrEmpty();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }


}